package com.bw.module_service.adapter

import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.module_service.R
import com.bw.module_service.model.GetSignInfoModel
import com.bw.module_service.model.GetSignInfoModelItem
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.tencent.mmkv.MMKV
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

class GetSignInfoAdapter(
    private val list: List<GetSignInfoModelItem>,
    private var onItemCLick:((GetSignInfoModel) -> Unit)? = null
) : RecyclerView.Adapter<GetSignInfoAdapter.GetSignInfoViewHolder>() {

    private val kv = MMKV.defaultMMKV()
    val login = kv.decodeDouble("login")
    val loginOut =  kv.decodeDouble("loginOut")
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GetSignInfoViewHolder {
        val view = View.inflate(parent.context, R.layout.item_signinfo,null)
        return GetSignInfoViewHolder(view)
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun onBindViewHolder(holder: GetSignInfoViewHolder, position: Int) {
        val currentTime = Date()
        val sdf = SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault())
        val formattedTime = sdf.format(currentTime)
        holder.tv1.text = formattedTime
        holder.tv3.text = list[position].location
        holder.tv2.text = list[position].status
        val morningDisplay = if (login != null) {
            "上班时间：${list[position].morningCheckIn}"
        } else {
            "上班还未打卡"
        }
        holder.goToWork.text = morningDisplay
        val afternoonDisplay = if (loginOut != null) {
            "下班时间：${list[position].afternoonCheckIn}"
        } else {
            "下班还未打卡"
        }
        holder.goToOutWork.text = afternoonDisplay
        if(holder.tv2.text == "正常") {
            val drawable = GradientDrawable().apply {
                cornerRadius = 30f
                setColor(Color.parseColor("#1A52C41A"))
            }
            holder.view_1.background = drawable
            holder.tv2.setTextColor(Color.parseColor("#FF52C41A"))
        } else if(holder.tv2.text == "迟到"){
            val drawable = GradientDrawable().apply {
                cornerRadius = 30f
                setColor(Color.parseColor("#1AF5A623"))
            }
            holder.view_1.background = drawable
            holder.tv2.setTextColor(Color.parseColor("#FFF5A623"))
        }else if(holder.tv2.text == "早退"){
            val drawable = GradientDrawable().apply {
                cornerRadius = 30f
                setColor(Color.parseColor("#1A1890FF"))
            }
            holder.view_1.background = drawable
            holder.tv2.setTextColor(Color.parseColor("#FF1890FF"))
        }else if(holder.tv2.text == "缺勤"){
            val drawable = GradientDrawable().apply {
                cornerRadius = 30f
                setColor(Color.parseColor("#1AFF4D4F"))
            }
            holder.view_1.background = drawable
            holder.tv2.setTextColor(Color.parseColor("#FFFF4D4F"))
        }else{
            val drawable = GradientDrawable().apply {
                cornerRadius = 30f
                setColor(Color.parseColor("#dc2626"))
            }
            holder.view_1.background = drawable
            holder.tv2.setTextColor(Color.parseColor("#ef4444"))
        }

        holder.itemView.setOnClickListener{
            ARouter.getInstance().build(Router.Ui.AttendancesActivity).navigation()
        }


    }


    class GetSignInfoViewHolder(
        itemView:View,
        ):RecyclerView.ViewHolder(itemView){
        val tv1:TextView = itemView.findViewById(R.id.tv_1)
        val goToWork = itemView.findViewById<TextView>(R.id.go_to_work)
        val tv3 = itemView.findViewById<TextView>(R.id.tv_3)
        val tv2:TextView = itemView.findViewById(R.id.tv2)
        val view_1:View = itemView.findViewById(R.id.view_1)
        val goToOutWork = itemView.findViewById<TextView>(R.id.go_toOut_work)
    }
}